package com.cn.validator;

import java.util.List;

import com.cn.model.User;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

public class GetUserValidator extends Validator {

	@Override
	protected void validate(Controller c) {
		
		validateRequired("ID", "message", "用户名不能为空！");
		validateRequired("password", "message", "密码不能为空！");
		String id = c.getPara("ID");
//		System.out.println(id);
		String password = c.getPara("password");
		List<User> listsuser = User.dao.find("select * from user where username='"+id+"'");
//		System.out.println(listsuser.size());
		if(listsuser.size()==0){
			addError("message", "用户名不存在！");
		}else{
			if(listsuser.get(0).get("password").equals(password)){
				c.setAttr("caozuo", true);
				c.setSessionAttr("user", id);
				c.setAttr("message", "/Shopping");
			}else{
				addError("message", "用户名或密码不正确！");
			}
		}
	}

	@Override
	protected void handleError(Controller c) {
		c.setAttr("caozuo", false);
		c.renderJson();
	}

}
